package com.hqyj.freemovie.front.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqyj.freemovie.front.domain.MovieTags;
import com.hqyj.freemovie.front.mapper.MovieTagsMapper;
import com.hqyj.freemovie.front.service.MovieTagsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.List;

@Service
public class MovieTagsServieImpl extends ServiceImpl<MovieTagsMapper, MovieTags> implements MovieTagsService {

    @Resource
    private MovieTagsMapper movieTagsMapper;

    @Transactional
    @Override
    public boolean saveMovieTag(MovieTags movieTags) throws Exception {
        try {
            movieTagsMapper.insert(movieTags);
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }
    }

    @Transactional
    @Override
    public boolean saveBatch(List<MovieTags> movieTagsList) throws Exception {
        try {
            for (MovieTags movieTags : movieTagsList) {
                movieTagsMapper.insert(movieTags);
            }
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return false;
        }

    }

    @Override
    public List<MovieTags> queryMovieTagsByMovieId(Integer id) {
        QueryWrapper<MovieTags> queryWrapper = new QueryWrapper<MovieTags>();
        queryWrapper.eq("m_id", id);

        return movieTagsMapper.selectList(queryWrapper);
    }
}
